import com.alibaba.fastjson.JSON;
import com.aliyun.odps.udf.UDF;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: lvfeng
 * @CreateTime: 2019-05-16 17:53
 * @Description:
 */
public class SpliderBeautyexpertGoods extends UDF {

    public  String evaluate(String html) {
        try {
            Map<String, String> map = new HashMap<>();
            /*Connection connect = Jsoup.connect("https://www.beautyexpert.com/benefit-ka-brow-mini-various-shades/11802784.html");
            Document doc = connect.get();*/
            Document doc = Jsoup.parse(html);
            //名称
            String title = doc.getElementsByClass("productName_title").text().trim();
            map.put("title", title);
            //价格
            Elements ePrice = doc.getElementsByClass("productPrice").get(0).getElementsByClass("productPrice_price");
            if (null != ePrice && ePrice.size() > 0){
                String price = ePrice.get(0).text();
                map.put("price", price);
            }
            /*for (int a=0;a<ePrice.size();a++){
                String schema = ePrice.get(a).attr("data-schema");
                if (schema.equalsIgnoreCase("price")){
                    String price = ePrice.get(a).text();
                    map.put("price", price);
                    break;
                }
            }*/
            //彩妆部分商品有色号，解析获取
            Elements colorEles = doc.getElementsByClass("productVariations_colorListItem");
            if (null != colorEles && colorEles.size() > 0){
                StringBuffer colorSb = new StringBuffer();
                for (Element colorEle :colorEles){
                    String color = colorEle.text();
                    colorSb = colorSb.append(color).append("@!@");
                }
                map.put("color", colorSb.substring(0,colorSb.lastIndexOf("@!@")));
            }

            //图片
            Elements imageWrapper = doc.getElementsByClass("productImageCarousel_imageWrapper");
            if (getFlag(imageWrapper,0)){

                StringBuffer sb=new StringBuffer();
                for (int a=0;a<imageWrapper.size();a++){
                    // System.out.println(imageWrapper.get(a));
                    Elements span = imageWrapper.get(a).getElementsByTag("span");
                    for (int b=0;b<span.size();b++){
                        String attr = span.get(b).attr("data-size");
                        //部分图片有1600 部分最大只有960  部分只有600
                        if (attr.contains("1600")){
                            sb.append("http:").append(span.get(b).attr("data-path")).append(",");
                            break;
                        }else if(attr.contains("960")) {
                        	sb.append("http:").append(span.get(b).attr("data-path")).append(",");
                            break;
                        }else if(attr.contains("600")) {
                        	sb.append("http:").append(span.get(b).attr("data-path")).append(",");
                            break;
                        }
                    }
                }
                //System.out.println(sb.toString());
                map.put("images", sb.toString());
            }
            //商品信息
            if (getFlag(doc, "productDescription_contentPropertyList", 0)) {
                Elements propertyList = doc.getElementsByClass("productDescription_contentPropertyList")
                        .get(0).getElementsByClass("productDescription_contentPropertyListItem");
                for (int a=0;a<propertyList.size();a++){
                    //判断title是否包含description
                    String text = propertyList.get(a).getElementsByClass("productDescription_contentPropertyHeading").text();
                    //System.out.println(text);
                    if (text.contains("Description")){
                        //因为描述内容可能有多段落，按照p分段
                        Elements elementsByTag = propertyList.get(a).getElementsByClass("productDescription_synopsisContent")
                                .get(0).getElementsByTag("p");
                        StringBuffer sb = new StringBuffer();
                        if(null != elementsByTag && elementsByTag.size()>0){
                            for (Element p :elementsByTag){
                                sb = sb.append(p.text()).append("@@");
                            }
                        }
                        String Description = sb.substring(0,sb.lastIndexOf("@@"));
                        map.put("Description", Description);
                    }else if (text.contains("Product Details")){
                        Elements contentWrapper = propertyList.get(a).getElementsByClass("productDescription_contentWrapper");
                        for (int b = 0; b < contentWrapper.size(); b++) {
                            String name = contentWrapper.get(b).getElementsByClass("productDescription_contentPropertyName")
                                    .text().replace(":", "").trim();
                            String value = contentWrapper.get(b).getElementsByClass("productDescription_contentPropertyValue").text().trim();
                            map.put(name, value);
                        }
                    }
                }
            }
            return JSON.toJSONString(map);
        } catch (Exception e) {
            e.printStackTrace();
            return "解析错误" + e.getMessage();
        }
    }

    private static boolean getFlag(Document doc, String className, int index) {
        try {
            doc.getElementsByClass(className).get(index);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    private static boolean getFlag(Element element, String className, int index) {
        try {
            element.getElementsByClass(className).get(index);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    private static boolean getFlag(Elements elements,int index) {
        try {
            elements.get(index);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
